package com.rental.quickenPoint.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rental.model.entity.UserAccount;
import com.rental.quickenPoint.controller.AliPayController;
import com.rental.quickenPoint.mapper.UserAccountMapper;
import com.rental.quickenPoint.service.IUserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.rental.model.entity.chargeRecord.ChargeRecord;
import com.rental.model.entity.user.UserInfo;
import com.rental.quickenPoint.mapper.ChargeRecordMapper;
import com.rental.user.client.UserInfoFeignClient;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hjk
 * @since 2025-01-16
 */
@Service
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements IUserAccountService {

    @Autowired
    private UserAccountMapper userAccountMapper;

    @Override
    public Boolean initUserAccount(Long userId) {
        LambdaQueryWrapper<UserAccount> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserAccount::getUid, userId);
        UserAccount userAccount = userAccountMapper.selectOne(lambdaQueryWrapper);
        if (userAccount != null) {
            return false;
        }
        userAccount = new UserAccount();
        userAccount.setUid(userId);
        userAccount.setQuickenPoint(0);
        userAccount.setCreateTime(new Date());
        userAccount.setUpdateTime(new Date());
        userAccountMapper.insert(userAccount);
        return true;
    }

    @Autowired
    private ChargeRecordMapper chargeRecordMapper;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Override
    public Boolean invite(ArrayList<Long> list, Long userId) {
        //根据list集合内的id获取所有用户信息
        List<UserInfo> userInfoList = userInfoFeignClient.getAllUserInfo().getData();
        Iterator<UserInfo> iterator = userInfoList.iterator();
        while (iterator.hasNext()) {
            UserInfo userInfo = iterator.next();
            if (!list.contains(userInfo.getId())) {
                iterator.remove();
            } else if (userInfo.getInviteStatus() == 1) {
                iterator.remove();
            }
        }
        if(userInfoList.size() == 0){
            return false;
        }
        System.out.println(userInfoList);
        for (UserInfo userInfo : userInfoList) {
            if(userInfo.getInviteStatus() == 1){
                return false;
            }
        }
        //遍历list集合把每个用户的状态改为1

        for (UserInfo userInfo : userInfoList) {
            userInfo.setInviteStatus(1);
            userInfoFeignClient.updateUserInfoById(userInfo);
        }
        LambdaQueryWrapper<UserAccount> userAccountLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userAccountLambdaQueryWrapper.eq(UserAccount::getUid,userId);
        UserAccount userAccount = userAccountMapper.selectOne(userAccountLambdaQueryWrapper);
        if (userAccount == null){
            return false;
        }
        //添加邀请人加速值
        userAccount.setQuickenPoint(userAccount.getQuickenPoint()+20);
        userAccountMapper.updateById(userAccount);
        //添加记录信息
        ChargeRecord chargeRecord = new ChargeRecord();
        UserInfo userById = userInfoFeignClient.getUserById(userId).getData();
        chargeRecord.setUid(userId);
        chargeRecord.setActivity("邀请好友");
        chargeRecord.setChangedValue(userById.getRealName()+"邀请好友获得20加速值");
        chargeRecord.setCreateTime(new Date());
        chargeRecord.setUpdateTime(new Date());
        chargeRecord.setIsDelete(0);
        chargeRecordMapper.insert(chargeRecord);
        return true;
    }

    @Override
    public List<UserInfo> getUserInfoExceptThis(Long userId) {
        List<UserInfo> list = userInfoFeignClient.getAllUserInfo().getData();
        list.removeIf(userInfo -> userInfo.getId().equals(userId));
        return list;
    }

    @Override
    public Integer getUserAccount(Long userId) {
        LambdaQueryWrapper<UserAccount> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserAccount::getUid,userId);
        UserAccount userAccount = userAccountMapper.selectOne(lambdaQueryWrapper);
        if (userAccount == null){
            return 0;
        }
        return userAccount.getQuickenPoint();
    }

    @Override
    public UserAccount selectByOrderNo(String payCode) {
        //查询订单信息
        return userAccountMapper.selectByOrderNo(payCode);
    }

    @Override
    public String payMoney(UserAccount userAccount) {
        LambdaQueryWrapper<UserAccount> eq = new LambdaQueryWrapper<UserAccount>().eq(UserAccount::getUid, userAccount.getUid());
        UserAccount account = userAccountMapper.selectOne(eq);
        userAccount.setId(account.getId());
        long currentedTimeMillis = System.currentTimeMillis();
        int length = String.valueOf(currentedTimeMillis).length();
        int n = 22 - length;
        Random random = new Random();
        StringBuilder randomPart = new StringBuilder();
        for (int i = 0; i < n; i++) {
            int index = random.nextInt(10);
            randomPart.append(index);
        }
        String r = String.valueOf(currentedTimeMillis) + randomPart.toString();
        userAccount.setPayCode(r);
        userAccount.setQuickenPoint(null);
        updateById(userAccount);
        return userAccount.getPayCode();
    }
    public Boolean sendAccelerateValueByAddInfo(Long userId) {
        LambdaQueryWrapper<UserAccount> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserAccount::getUid, userId);
        UserAccount userAccount = userAccountMapper.selectOne(lambdaQueryWrapper);
        if (userAccount == null){
            return false;
        }
        userAccount.setQuickenPoint(userAccount.getQuickenPoint()+20);
        userAccountMapper.updateById(userAccount);
        ChargeRecord chargeRecord = new ChargeRecord();
        chargeRecord.setUid(userId);
        chargeRecord.setActivity("发布信息");
        chargeRecord.setChangedValue("Id为"+userId+"的用户通过发布信息获得加速值"+20);
        chargeRecord.setCreateTime(new Date());
        chargeRecord.setUpdateTime(new Date());
        chargeRecord.setIsDelete(0);
        chargeRecordMapper.insert(chargeRecord);
        return true;
    }


}
